import {
    Color,
    Engine3D,
    Scene3D,
    Object3D,
    Camera3D,
    View3D,
    LitMaterial,
    BoxGeometry,
    MeshRenderer,
    DirectLight,
    HoverCameraController,
    AtmosphericComponent,
    Vector3
} from '@orillusion/core';

export class Editor {
    // 世界场景
    globalScene = null as unknown as Scene3D
    // 世界视图
    globalView = null as unknown as View3D

    constructor(width = 600, height = 400) {
    }
    async init(canvasId?: string) {
        await this.initEngine(canvasId ? document.getElementById(canvasId) as HTMLCanvasElement : undefined);
        await this.initScene();
    }
    async initEngine(canvas?: HTMLCanvasElement) {
        if (canvas) {
            await Engine3D.init({ canvasConfig: { canvas} });
        } else {
            await Engine3D.init();
        }
    }
    async initScene() {
        this.globalScene = new Scene3D();
        // Add atmospheric scattering skybox component
        let sky = this.globalScene.addComponent(AtmosphericComponent);
        // Create a camera object
        let cameraObj = new Object3D();
        let camera = cameraObj.addComponent(Camera3D);
        // Set the camera perspective according to the window size
        camera.perspective(60, window.innerWidth / window.innerHeight, 1, 5000.0);
        // Set camera controller
        let controller = camera.object3D.addComponent(HoverCameraController);
        controller.setCamera(0, 0, 15);
        // Add camera node to sence
        this.globalScene.addChild(cameraObj);
        // Create a light object
        let light = new Object3D();
        // Add direct light component
        let component = light.addComponent(DirectLight);
        // Adjust light parameters
        light.rotationX = 45;
        light.rotationY = 30;
        component.intensity = 2;
        // Add light node to sence
        this.globalScene.addChild(light);
        // Create View3D object
        let view = new View3D();
        // Specify the scene to render
        view.scene = this.globalScene;
        // Specify the camera to use
        view.camera = camera;
        // Start rendering
        Engine3D.startRenderView(view);
    }
    async add3DCube(positon = new Vector3(0, 0, 0), size = new Vector3(1, 1, 1)) {
        // Create a new object
        const obj = new Object3D();
        // Add MeshRenderer to object(obj)
        let mr = obj.addComponent(MeshRenderer);
        // Set geometry
        mr.geometry = new BoxGeometry(...size.toArray());
        // Set postion
        mr.transform.localPosition = positon;
        // Set material
        mr.material = new LitMaterial();
        this.globalScene.addChild(obj);
    }
    async addModelByGltf(fileurl: string, positon = new Vector3(0, 0, 0), size = new Vector3(1, 1, 1)) {
        // 加载 gltf 文件
        console.log(fileurl)
        let data = await Engine3D.res.loadGltf(fileurl);
        data.localPosition = positon;
        // 添加至场景
        this.globalScene.addChild(data);
    }
    async Dispose(){
       // Engine3D.resume();
    }
}